package cn.tedu.day01.jnsdlfaq.user.controller;

import cn.tedu.day01.jnsdlfaq.user.service.SysUserService;
import cn.tedu.day01.jnsdlfaq.util.AjaxResult;
import cn.tedu.day01.jnsdlfaq.user.pojo.SysUser;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户控制层
 * 杨景朝
 */
@RestController
@RequestMapping("/user/")
@CrossOrigin(origins = "*", maxAge = 3600)//跨域
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    //查询所有用户
    @RequestMapping("list/{page}/{size}")
    public AjaxResult selectAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<SysUser> sysUserPage = new Page<>(page, size);
        IPage<SysUser> teacherIPage = sysUserService.selectUserAll(sysUserPage);
        return AjaxResult.success(teacherIPage);
    }

    //添加用户
    @RequestMapping("add")
    public AjaxResult addUser(@RequestBody SysUser user) {
        int row = sysUserService.addUser(user);
        return AjaxResult.success(row);
    }

    //根据id查询用户
    @RequestMapping("selectById/{id}")
    public AjaxResult selectById(@PathVariable String id) {
        SysUser user = sysUserService.selectById(Integer.parseInt(id));
        return AjaxResult.success(user);
    }

    @RequestMapping("deleteById/{userId}")
    public AjaxResult deleteUserById(@PathVariable("userId") String userId) {
        System.out.println(userId + "=====");
        int row = sysUserService.deleteUser(Integer.parseInt(userId));
        return AjaxResult.success(row);
    }

    @PostMapping("updateUser")
    public AjaxResult updateUser(SysUser user) {
        int row = sysUserService.updateUser(user);
        return AjaxResult.success(row);
    }
}
